package com.example.easybuy.config;

import com.example.easybuy.listen.RedisListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisConfig {
    private final RedisMessageListenerContainer redisMessageListenerContainer;//Spring提供的用于管理Redis消息监听器的容器。
    private final RedisConnectionFactory redisConnectionFactory;//用于创建Redis连接的工厂。
    private final RedisListener redisListener;//自定义的消息监听器，用于处理接收到的消息。


    public RedisConfig(RedisConnectionFactory redisConnectionFactory, RedisListener redisListener) {
        this.redisConnectionFactory = redisConnectionFactory;
        this.redisListener = redisListener;
        this.redisMessageListenerContainer = new RedisMessageListenerContainer();
    }

    @Bean
    public RedisMessageListenerContainer messageListenerContainer() {
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(redisListener, new PatternTopic("__keyevent@0__:expired"));//监听的模式，过期事件监听
        return redisMessageListenerContainer;
    }
}
